package com.wedu.service.impl;

import com.wedu.dao.StudentDao;
import com.wedu.dao.impl.StudentDaoImpl;
import com.wedu.entity.Student;
import com.wedu.service.StudentService;

import java.util.Scanner;


public class StudentServiceImpl implements StudentService {

    //创建StudentDaoImpl对象
    private StudentDao studentDao = new StudentDaoImpl();

    @Override
    public boolean loginStu(String name,String password) {
        if(name == null || password == null){
            return false;
        }
        Student login = studentDao.login(name, password);
        if (login!=null){
            return true;
        }else
            return false;
    }

    @Override
    public boolean register(String name,String sex,int age,String password) {
        if (name == null || password == null||sex==null||age==0){
            System.out.println("输入不能为空");
            return false;
        }
        if (!sex.equals("男")&&!sex.equals("女")){
            System.out.println("性别只能为男或女");
            return false;
        }
        int i = studentDao.register(name, sex, age, password);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Student findOneById(int id) {
        return studentDao.findOneById(id);
    }

    @Override
    public Student deleteStu(int id) {
        return studentDao.deleteStu(id);
    }

    @Override
    public Student updateMsg(int id,String name,int age,String sex,String password) {
        if (name == null || password == null||sex==null||age==0){
            System.out.println("输入不能为空");
            return null;
        }
        if (!sex.equals("男")&&!sex.equals("女")) {
            System.out.println("性别只能为男或女");
            return null;
        }
        return studentDao.updateMsg(id,name,age,sex,password);
    }

    @Override
    public int inputNumber() {
        while (true) {
            Scanner sc = new Scanner(System.in);
            if (sc.hasNextInt()) {
                return sc.nextInt();
            }else {
                System.out.println("数字输入错误请重新输入：");
            }
        }
    }
}
